<?php
$title = "Contacts - Code4Fun.org";		// set title 
include 'includes/top.php';				// print top 
include 'includes/submenu.php';			// print submenu
require_once("c4fmailer/ValidationSet.php");
require_once("c4fmailer/NoEmptyValidator.php");
require_once("c4fmailer/EmailFormatValidator.php");
require_once("c4fmailer/EmailHeaderInjectionValidator.php");
require_once("c4fmailer/c4fmailer.php");

// set errors
$invalidUserName			= "Missing Name";
$invalidSenderMail			= "Missing or Invalid E-mail";
$invalidSubject				= "Missing Subject";
$injectionAttemptInSubject	= "Injection attempt detected in Subject";
$invalidMessage				= "Missing Message";

?>

	<div id="content" >
		<h2>Contacts</h2>
		<p>If you have any question that is not answered or explained throughout the website or the <acronym title="Frequently Asked Questions" >FAQ</acronym> sections, please use the form below to make contact.</p>
<?php

if ($_SERVER['REQUEST_METHOD']=='POST') {
	
	$validationset = new ValidationSet();
	$validationset->addValidator(new NoEmptyValidator("username", $_POST['username'], $invalidUserName));
	$validationset->addValidator(new EmailFormatValidator("sender", $_POST['sender'], $invalidSenderMail));
	$validationset->addValidator(new NoEmptyValidator("subject", $_POST['subject'], $invalidSubject));
	$validationset->addValidator(new EmailHeaderInjectionValidator("subject", $_POST['subject'], $injectionAttemptInSubject));
	$validationset->addValidator(new NoEmptyValidator("message", $_POST['message'], $invalidMessage));
	
	$result = false;
	if ($validationset->performValidation()) {
		$mail = new c4fmailer();
		try {
			if ($mail->send($_POST['username'],$_POST['sender'], "info@code4fun.org", "[C4F Contacts] ".$_POST['subject'], $_POST['message'])) {
				$result = true;
				print "		<blockquote id=\"positiveOutcome\" >\n			<p>Message delivered successfully.</p>\n		</blockquote>\n";							
			} else {
				print "		<blockquote id=\"negativeOutcome\" >\n			<p>Message was NOT successfully accepted for delivery.</p>\n		</blockquote>\n";
			}
		} catch (Exception $e) { /* This does not appear unless validation is disabled */
			print "		<blockquote id=\"negativeOutcome\" >\n			<p>Error: ".$e->getMessage()."</p>\n		</blockquote>\n";
		}
	} else {
		print "		<blockquote id=\"negativeOutcome\" >\n			<h3>Error(s):</h3>\n			<ul>\n";
		$errors = $validationset->getErrors();
		foreach ($errors as $e) {
			print "				<li>".$e->getErrorMessage()."</li>\n";
		}
		print "			</ul>\n		</blockquote>\n";
	}
}

?>
		<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" id="contacts" >
			<fieldset>
				<legend>Name</legend>
				<label for="name" >Name:&nbsp;</label>
				<input id="name" type="text" name="username" size="30" value="<?php print ($result) ? "" : c4fmailer::safeStripSlashes($_POST['username']); ?>" />
			</fieldset>
			<fieldset>
				<legend>E-mail</legend>
				<label for="email" >E-mail:&nbsp;</label>
				<input id="email" name="sender" type="text" size="30" value="<?php print ($result) ? "" : c4fmailer::safeStripSlashes($_POST['sender']); ?>" />
			</fieldset>
			<fieldset>
				<legend>Subject</legend>
				<label for="subject" >Subject:&nbsp;</label>
				<input id="subject" name="subject" type="text" size="30" value="<?php print ($result) ? "" : c4fmailer::safeStripSlashes($_POST['subject']); ?>" />
			</fieldset>
			<fieldset>
				<legend>Message</legend>
				<label>Message:&nbsp;</label>
				<textarea cols="30" rows="8" name="message"><?php print ($result) ? "" : c4fmailer::safeStripSlashes($_POST['message']); ?></textarea>
			</fieldset>
			<fieldset id="send" >
				<legend>Send</legend>
				<input type="submit" name="submit" value="Submit" size="14" />
			</fieldset>
		</form>
<?php include 'includes/gotop.php'; ?>
	</div>
<?php include 'includes/foot.php'; /*print page footer*/ ?>
